﻿using Model;

namespace Dal
{
    public class memberDal
    {
        private readonly MyDbContext db;

        public memberDal(MyDbContext db)
        {
            this.db = db;
        }

        /// <summary>
        /// 会员信息录入
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int PostMemberAdd(T_MemberInfo info)
        {
            try
            {
                db.T_MemberInfo.Add(info);
                return db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 查询数据显示
        /// </summary>
        /// <param name="name">会员姓名</param>
        /// <param name="phone">手机号</param>
        /// <param name="card">身份证号</param>
        /// <param name="code">卡号</param>
        /// <param name="createDate">创建时间</param>
        /// <returns></returns>
        public List<T_MemberInfo> GetMemberList(string ?name,string ?phone,string ?card,string ?code,DateTime createDate)
        {
            try
            {
                List<T_MemberInfo> list = db.T_MemberInfo.ToList();
                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(x=>x.M_Name.Contains(name)).ToList();
                }
                if (!string.IsNullOrEmpty(phone))
                {
                    list = list.Where(x=>x.M_Phone.Contains(phone)).ToList();
                }
                if (!string.IsNullOrEmpty(card))
                {
                    list = list.Where(x=>x.M_Card.Contains (card)).ToList();
                }
                if (!string.IsNullOrEmpty(code))
                {
                    list = list.Where(x=>x.M_Code == code).ToList();
                }
                return list;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 手机号唯一性
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public int SearchPhone(string phone)
        {
            try
            {
                int list = db.T_MemberInfo.Where(x => x.M_Phone == phone).Count();
                return list;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}